<?php

namespace App\Http\Controllers\Admin;

use App\Game;
use App\GameConfig;
use App\GamePrize;
use App\GamePrizeRecord;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GameController extends Controller
{

    //
    public function index(Request $request){
        return view('admin.game.list');
    }
    public function ajax_list(Request $request){

        $model= GameConfig::with('gamePrize');

        if($request->searchKey){
            $model=$model->where('name','like', '%'.$request->searchKey.'%');
        }
        $model=$model->paginate($request->limit)
            ->toArray();
        return $this->page_json_render($model);
    }
    //编辑
    public function edit(Request $request,$id){
        $model=GameConfig::findOrFail($id);
        $prize=GamePrize::where('game_id',1)->get();
        return view('admin.game.edit_add',compact('id','model','prize'));
    }
    public function update(Request $request){
        $id=$request->id;
        $this->filter($request);
        $model=GameConfig::findOrFail($id);
        $model->prize_id=$request->prize_id;
        $model->chance=$request->chance;

        $model->save();
        return redirect()->route('admin.game_index')->with(
            $this->success()
        );
    }

    protected function filter($request){
        $this->validate($request,
            [
                'chance'=>'required',
                'prize_id'=>'required'
            ]);
    }
    public function record(Request $request){
        return view('admin.record.list');
    }
    public function record_ajax_list(Request $request){

        $model= GamePrizeRecord::with('member');

        if($request->searchKey){
            $model=$model->where('name','like', '%'.$request->searchKey.'%');
        }
        $model=$model->orderBy('id','desc')->paginate($request->limit)
            ->toArray();
        return $this->page_json_render($model);
    }
    public function guize(){
        $game=Game::find(1);
        return view('admin.game.guize',compact('game'));
    }
}
